#include "Dojo.h"
#include "Sequence.h"
#include <iostream>
using namespace std;

Dojo::Dojo()
{
}

void Dojo::Start()
{
	Sequence AI;
	AI.Generate();

	Sequence player;
	for (int counter = 0; counter < NOOF_MOVES; ++counter)
	{
		int key;
		cout << "Attack #" << counter << " (1=high, 2=med, 3=low): ";
		cin >> key;

		MovePosition position;
		switch (key)
		{
		case 1:
			position = HIGH;
			break;
		case 2:
			position = MID;
			break;
		case 3:
		default:
			position = LOW;
			break;
		};

		player.SetMove(counter, ATTACK, position);
	}
	for (int counter = 0; counter < NOOF_MOVES; ++counter)
	{
		int key;
		cout << "Block #" << counter << " (1=high, 2=med, 3=low): ";
		cin >> key;

		MovePosition position;
		switch (key)
		{
		case 1:
			position = HIGH;
			break;
		case 2:
			position = MID;
			break;
		case 3:
		default:
			position = LOW;
			break;
		};

		player.SetMove(counter, BLOCK, position);
	}

	FightWinner winner = Fight(&player, &AI);
	switch (winner)
	{
	case Player1:
		cout << "Player 1 wins!" << endl;
		break;
	case Player2:
		cout << "Player 2 wins!" << endl;
		break;
	case Draw:
		cout << "Draw!" << endl;
		break;
	};
}

FightWinner Dojo::Fight(Sequence * i_player1, Sequence * i_player2) const
{
	unsigned int player1wins = 0;
	unsigned int player2wins = 0;

	for (int counter = 0; counter < NOOF_MOVES; ++counter)
	{
		unsigned char player1attack = i_player1->GetAttack(counter);
		unsigned char player2attack = i_player2->GetAttack(counter);
		unsigned char player1block = i_player1->GetBlock(counter);
		unsigned char player2block = i_player2->GetBlock(counter);

		if ((player1attack&LOW)&&(!(player2block&LOW)) ||
			(player1attack&MID)&&(!(player2block&MID)) ||
			(player1attack&HIGH)&&(!(player2block&HIGH)))
		{
			++player1wins;
		}

		if ((player2attack&LOW)&&(!(player1block&LOW)) ||
			(player2attack&MID)&&(!(player1block&MID)) ||
			(player2attack&HIGH)&&(!(player1block&HIGH)))
		{
			++player2wins;
		}

		cout << "Player 1 (" << 100-(player2wins*20) << ") & Player 2 (" << 100-(player1wins*20) << ")" << endl;
	}

	if (player1wins>player2wins)
		return Player1;
	else if (player2wins>player1wins)
		return Player2;
	else
		return Draw;
}

